package com.baidu.bulls.stock.admin.user.controller;

import com.baidu.bulls.stock.admin.user.service.AuthorityLogicService;
import com.baidu.bulls.stock.common.exception.BusinessException;
import com.baidu.bulls.stock.entity.user.AuthorityRole;
import com.baidu.bulls.stock.entity.user.AuthorityUser;
import com.baidu.bulls.stock.entity.user.AuthorityUserRole;
import com.baidu.stock.common.web.vo.ApiRespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/authorityLogic")
@Slf4j
@Api(value = "AuthorityLogicController",tags = {"权限相关"})
public class AuthorityLogicController extends BaseController {
    @Autowired
    private AuthorityLogicService authorityLogicServiceImpl;


    @ApiOperation("保存角色权限接口")
    @PostMapping("/savePrivileges")
    public ApiRespResult savePrivileges(@RequestParam("roleAuth") String roleAuth,
                                        @RequestParam("roleIds") String... roleIds) {
        ApiRespResult result = null;
        ApiRespResult apiRespResult = ApiRespResult.success();
        try {
            authorityLogicServiceImpl.savePrivileges(roleAuth, roleIds);
            result = ApiRespResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result = ApiRespResult.sysError(e.getMessage());
        }
        return result;
    }

    @PostMapping("/userAddRoles")
    @ApiOperation("分配用户与角色的关联关系接口")
    public ApiRespResult userAddRoles(AuthorityUserRole authorityUserRole) {
        ApiRespResult result = null;
        try {
            AuthorityUser loginUser = getUser();
            authorityLogicServiceImpl.userAddRoles(authorityUserRole, loginUser);

            result =  ApiRespResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result = ApiRespResult.sysError(e.getMessage());
        }
        return result;
    }
}
